/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.controller.pc;

import java.util.List;

import lineage.database.tables.CharactersTable;
import lineage.model.instance.L1PcInstance;
import lineage.util.collections.Lists;

/**
 * 在线角色.
 * 
 * @version 2014年3月23日上午11:59:35
 * @author jrwz
 */
public final class OnlineList {
    private final List<String> onlineList = Lists.newArrayList();

    /**
     * 静态初始化器，由JVM来保证线程安全.
     */
    private static class Holder {
        static OnlineList instance = new OnlineList();
    }

    /**
     * 取得该类的实例.
     */
    protected static final OnlineList getInstance() {
        return Holder.instance;
    }

    private OnlineList() {
    }

    /**
     * 检查是否在线(根据角色名).
     * 
     * @param name
     *            - 要检查的角色名称
     * @return 如果在线则返回true
     */
    protected final boolean isOnline(final String name) {
        if (onlineList.contains(name)) {
            return true;
        }
        return false;
    }

    /**
     * 检查是否在线.
     * 
     * @param pc
     *            - 要检查的角色
     * @return 如果在线则返回true
     */
    protected final boolean isOnline(final L1PcInstance pc) {
        return isOnline(pc.getName());
    }

    /**
     * 将角色‘加入’在线列表.
     * 
     * @param name
     *            - 要加入的角色名称
     * @return 如果成功加入则返回true
     */
    protected final boolean add(final String name) {
        if (!isOnline(name)) {
            onlineList.add(name);
            return true;
        }
        return false;
    }

    /**
     * 将角色‘加入’在线列表.
     * 
     * @param pc
     *            - L1PcInstance对象
     * @return 如果成功加入则返回true
     */
    protected final boolean add(final L1PcInstance pc) {
        final String name = pc.getName();
        if (!isOnline(name)) {
            onlineList.add(name);
            return true;
        }
        return false;
    }

    /**
     * 将角色‘移出’在线列表.
     * 
     * @param name
     *            - 要移出的角色名称
     * @return 如果成功移出则返回true
     */
    protected final boolean remove(final String name) {
        if (isOnline(name)) {
            onlineList.remove(name);
            return true;
        }
        return false;
    }

    /**
     * 将角色‘移出’在线列表.
     * 
     * @param pc
     *            - 要移出的角色
     * @return 如果成功移出则返回true
     */
    protected final boolean remove(final L1PcInstance pc) {
        return remove(pc.getName());
    }

    /**
     * 根据角色名取得在线角色数据.
     * 
     * @param name
     *            - 角色名
     * @return L1PcInstance对象(如果没在线或找不到则返回null)
     */
    protected final L1PcInstance getPcOnline(final String name) {
        if (isOnline(name)) {
            return CharactersTable.getInstance().getPc(name);
        }
        return null;
    }

    /**
     * 取得在线角色数量.
     * 
     * @return 总数量
     */
    protected final int getCount() {
        return onlineList.size();
    }

    /**
     * 取得在线列表.
     * 
     * @return 列表
     */
    protected final List<String> getList() {
        return onlineList;
    }
}
